﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_AddType : ATPages
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hidID1.Value = Request.QueryString["moduid"].ToString();
            lblTime.Text = DateTime.Now.ToShortDateString();
            lblUser.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
            lblwz.Text = f.getModuleidAdd(Request["moduid"]);
            
            if (Request.QueryString["action"].ToString()=="update")
            {
               
                hidID1.Value = Request.QueryString["moduid"].ToString();
                hidId.Value = Request.QueryString["id"].ToString();
                txtprice.Attributes["contentEditable"] = "false";
                btnSave.Text = " 修改 ";
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            }
            if (Request.QueryString["action"].ToString() == "flag")
            {
                ZSql flag = new ZSql();
                hidID1.Value = Request.QueryString["moduid"].ToString();
                hidId.Value = Request.QueryString["id"].ToString();
           
                btnSave.Text = " 审核 ";
                flag.Open("select * from CS_Club where id='" + hidId.Value + "' ");
                if (flag.m_table.Rows[0]["AuditState"].ToString() == "1")
                {
                    rbtnFlag.Checked = true;
                    rbtnnotFlag.Checked = false;
                }
                if (flag.m_table.Rows[0]["AuditState"].ToString() == "0")
                {
                    rbtnFlag.Checked = false;
                    rbtnnotFlag.Checked = true;
                }
            }
            if (Request.QueryString["action"].ToString() == "add")
            {
              
                btnSave.Text = " 设置 ";
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            }
            
            initType();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
       
            ZSql zs = new ZSql();
            if (!string.IsNullOrEmpty(hidId.Value))
            {
                int flag;
                if (rbtnFlag.Checked)
                {
                    flag = 1;//5+1+a+s+p+x
                }
                else
                {
                    flag = 0;
                }
                if (Request.QueryString["action"].ToString() == "flag")
                {

                    zs.Open("update CS_Club set AuditState=" + flag + " where id=" + hidId.Value);
                    Response.Write("<script>window.location='ClubHYpriceMange.aspx?moduid=" + Request["moduid"] + "';</script>");

                }

                else
                {
                    

                    zs.Open("update CS_Club set hy_name='" + Common.FormatParameter(txtname.Text) + "',hy_price=" + Convert.ToDouble(Common.FormatParameter(txtprice.Text)) + ",hy_convent='" + Common.FormatParameter(txtcontent.Text) + "' where id=" + hidId.Value);
                   ZSql uphy = new ZSql();
                   ZSql sz = new ZSql();
                   ZSql up = new ZSql();
                   sz.Open("select * from CS_ClupMenber where clup_zid="+hidId.Value+"");
                   if (sz.Rows.Count > 0)
                   {
                       //for (int i = 0; i < sz.Rows.Count; i++)
                       //{

                       //    if (Convert.ToDouble(sz.m_table.Rows[i]["clup_tolmoney"]) < Convert.ToDouble(txtprice.Text))
                       //    {
                       //        up.Open("update CS_ClupMenber set clup_zid=0 where clup_id=" + Convert.ToInt32(sz.Rows[i]["clup_id"]) + "");
                       //    }

                       //}
                       Response.Write("<script>window.location='ClubHYpriceMange.aspx?moduid=" + Request["moduid"] + "';</script>");
                   }
                   else
                   {
                       ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('客户未购货！');", true);
                       return;
                   }
                   
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                   
                }
             
               
                
            
               

            }
            else
            {
                ZSql sz = new ZSql();
                ZSql up = new ZSql();
                ZSql add = new ZSql();
                add.Open("select * from CS_Club where hy_name='" + Common.FormatParameter(txtname.Text) + "'");
                if (add.m_table.Rows.Count > 0)
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('该会员组名已存在！');", true);
                    return;
                }
                string hyid = add.GetScalar("insert into CS_Club (hy_name,hy_price,hy_convent,EditUserName,EditDate)values('" + Common.FormatParameter(txtname.Text) + "'," + Convert.ToDouble(Common.FormatParameter(txtprice.Text)) + ",'" + Common.FormatParameter(txtcontent.Text) + "','"+lblUser.Text+"','"+lblTime.Text+"');select nid = @@identity").ToString();
                sz.Open("select count(*),sum(a.totalmoney) as totalmoney,Address,Phone,guestman from CS_Sales a join CS_Guest b on a.guestid=b.guestid group by guestman,Address,Phone");
                if (sz.Rows.Count > 0)
                {
                    for (int i = 0; i < sz.Rows.Count; i++)
                    {
                        if (Convert.ToDouble(sz.m_table.Rows[i]["totalmoney"]) >= Convert.ToDouble(txtprice.Text))
                        {
                            ZSql men = new ZSql();
                            men.Open("select * from CS_ClupMenber where clup_man='" + sz.m_table.Rows[i]["guestman"].ToString() + "'");
                            if (men.m_table.Rows.Count > 0)
                            {
                                up.Open("update CS_ClupMenber set clup_zid="+Convert.ToInt32(hyid)+" where clup_man='" + sz.m_table.Rows[i]["guestman"].ToString() + "'");
                            }
                            else
                            {
                                up.Open("insert into CS_ClupMenber (clup_zid,clup_man,clup_tel,clup_address,clup_tolmoney) values(" + Convert.ToInt32(hyid) + ",'" + sz.m_table.Rows[i]["guestman"].ToString() + "','" + sz.m_table.Rows[i]["Phone"].ToString() + "','" + sz.m_table.Rows[i]["Address"].ToString() + "'," + Convert.ToDouble(sz.m_table.Rows[i]["totalmoney"]) + ")");
                            }

                        }
                        else
                        {
                            ZSql men = new ZSql();
                            men.Open("select * from CS_ClupMenber where clup_man='" + sz.m_table.Rows[i]["guestman"].ToString() + "'");
                            if (men.m_table.Rows.Count > 0)
                            {

                            }
                            else
                            {
                                up.Open("insert into CS_ClupMenber (clup_zid,clup_man,clup_tel,clup_address,clup_tolmoney) values(0,'" + sz.m_table.Rows[i]["guestman"].ToString() + "','" + sz.m_table.Rows[i]["Phone"].ToString() + "','" + sz.m_table.Rows[i]["Address"].ToString() + "'," + Convert.ToDouble(sz.m_table.Rows[i]["totalmoney"]) + ")");
                            }
                        }

                    }
                    Response.Write("<script>window.location='ClubHYpriceMange.aspx?moduid=" + Request["moduid"] + "';</script>");
                }
                else
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('客户未购货！');", true);
                    return;
                }
            }
            zs.Close();
       
    }
    protected void Linkcannel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ClubHYpriceMange.aspx?moduid=" + hidID1.Value);
    }
    private void initType()
    {
        ZSql type = new ZSql();
        if (!string.IsNullOrEmpty(hidId.Value))
        {
            type.Open("select * from CS_Club where id=" + hidId.Value);
            if (type.m_table.Rows[0]["AuditState"].ToString() == "1")
            {
                rbtnnotFlag.Checked = false;
                rbtnFlag.Checked = true;
            }
            txtname.Text = type.m_table.Rows[0]["hy_name"].ToString();
            txtprice.Text = type.m_table.Rows[0]["hy_price"].ToString().Substring(0, type.m_table.Rows[0]["hy_price"].ToString().Length-2);
            txtcontent.Text = type.m_table.Rows[0]["hy_convent"].ToString();
            type.Close();
        }
    }
}
